<template>
  <div class="app-container page-container right-side-dialog">
    <!-- 表格 -->
    <div class="main-body" :style="{minHeight: height-25 + 'px'}">
      <!-- 操作按鈕 -->
      <div class="main-header">
        <el-row :gutter="20">
          <el-col :span="4" class="left">字典列表</el-col>
          <el-col :span="20" class="right">
            <span v-if="auth.save != -1" class="spp-btn spp-btn-primary" @click="dicEditVisible = true">新增字典</span>
            <span v-if="auth.cache != -1" class="spp-btn" @click="dicCache()">同步至缓存</span>
          </el-col>
        </el-row>
      </div>
      <el-table ref="table" :data="dic_all_table_data" :height="height-40" size="mini" stripe>
        <el-table-column label="序号" type="index" width="60" />
        <el-table-column label="字典编码" prop="dic_code" min-width="100" align="left" />
        <el-table-column label="字典名称" prop="dic_name" min-width="100" align="left" />
        <el-table-column label="字典描述" prop="dic_desc" min-width="100" align="left" />
        <el-table-column label="操作" width="150" fixed="right" align="left">
          <template slot-scope="scope">
            <el-button v-if="auth.update != -1" type="text" @click="dicUpdate(scope.row)">修改</el-button>
            <el-button type="text" class="font_red" v-if="scope.row.status_flag ==='1' && auth.disable != -1" @click="dicDisable(scope.row.dic_id)">停用</el-button>
            <el-button type="text" class="font_green" v-if="scope.row.status_flag ==='0' && auth.enable != -1" @click="dicEnable(scope.row.dic_id)">启用</el-button>
            <el-button v-if="auth.update != -1" type="text" @click="getTableData(scope.row)">条目</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增编辑字典 -->
    <el-dialog :title="dic_edit_form.title" :append-to-body="true" :visible.sync="dicEditVisible" width="720px" :close-on-click-modal="false">
      <el-form ref="dic_edit_form" label-width="0" :model="dic_edit_form">
        <el-form-item prop="dic_code">
          <div class="form_lable">字典编码</div>
          <el-input v-model="dic_edit_form.dic_code" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="dic_name">
          <div class="form_lable">字典名称</div>
          <el-input v-model="dic_edit_form.dic_name" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="dic_desc">
          <div class="form_lable">字典描述</div>
          <el-input v-model="dic_edit_form.dic_desc" type="textarea" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <span class="spp-btn" @click="dicEditVisible = false">取消</span>
        <span class="spp-btn spp-btn-primary" @click="dicEditSubmit()">保存</span>
      </span>
    </el-dialog>
    <!-- 条目 -->
    <el-dialog title="条目" :visible.sync="dicChildrenVisible" :close-on-click-modal="false" width="720px">
      <el-scrollbar wrap-class="vertical_scrollbar" :style="{height: height + 'px'}">
        <el-table ref="table" :data="dic_table_data" :height="height-50" size="mini" stripe>
          <el-table-column label="序号" type="index" width="60" />
          <el-table-column label="字典编码" prop="dic_code" />
          <el-table-column label="字典名称" prop="dic_name" />
          <el-table-column label="字典描述" prop="dic_desc" />
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button v-if="auth.update != -1" type="text" @click="dicUpdate(scope.row)">修改</el-button>
              <el-button type="text" class="font_red" v-if="scope.row.status_flag ==='1' && auth.disable != -1" @click="dicDisable(scope.row.dic_id)">停用</el-button>
              <el-button type="text" class="font_green" v-if="scope.row.status_flag ==='0' && auth.enable != -1" @click="dicEnable(scope.row.dic_id)">启用</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-scrollbar>
      <div class="dialog-btn" style="text-align: right">
        <span class="spp-btn" @click="dicChildrenVisible = false">取消</span>
        <span class="spp-btn spp-btn-primary" @click="dicEditVisible = true">添加新条目</span>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { dicList, save, update, cache, disable, enable } from '@/api/pajx/sys/dic'
import { mapState } from 'vuex'
export default {
  name: 'SysDic',
  components: { },
  data() {
    return {
      height: window.innerHeight - 135,
      auth: {},
      select_dic_data: {},
      dic_all_table_data: [],
      dicChildrenVisible: false,
      dic_table_data: [],
      dicEditVisible: false,
      dic_edit_form: {
        title: '新增字典',
        type: 'add',
        dic_code: '',
        dic_name: '',
        dic_pcode: '',
        dic_desc: '',
        dic_id: ''
      }
    }
  },
  computed: {
    ...mapState({
      permission: state => state.permission.permission
    })
  },
  watch: {
    dicChildrenVisible(val) {
      if (!val) {
        this.select_dic_data = {}
      }
    },
    dicEditVisible(val) {
      if (val) {
        this.dic_edit_form.title = this.dic_edit_form.type==='add'?'新增字典':'编辑字典'
      }
      if (!val) {
        this.dic_edit_form.dic_code = ''
        this.dic_edit_form.dic_name = ''
        this.dic_edit_form.dic_pcode = ''
        this.dic_edit_form.dic_desc = ''
        this.dic_edit_form.dic_id = ''
        this.dic_edit_form.type = 'add'
      }
    }
  },
  methods: {
    // 获取按钮权限
    permissionAuth() {
      this.auth.save = this.permission.indexOf('/api/sys/dic/save')
      this.auth.update = this.permission.indexOf('/api/sys/dic/update')
      this.auth.cache = this.permission.indexOf('/api/sys/dic/cache')
      this.auth.enable = this.permission.indexOf('/api/sys/dic/enable')
      this.auth.disable = this.permission.indexOf('/api/sys/dic/disable')
    },
    // 获取字典列表
    getDicList() {
      var dic_code = this.select_dic_data.dic_code
      dicList(dic_code).then(response => {
        if(!dic_code) {
         this.dic_all_table_data = response.data
        } else {
          this.dic_table_data = response.data
        }
      })
    },
    getTableData(data) {
      this.select_dic_data = data
      this.dicChildrenVisible = true
      this.getDicList()
    },
    // 新增编辑字典
    dicEditSubmit() {
      // 新增
      if (this.dic_edit_form.type === 'add') {
        this.dic_edit_form.dic_pcode = this.select_dic_data.dic_code
        save(this.dic_edit_form).then(response => {
          this.dicEditVisible = false
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.getDicList()
        })
      }
      // 修改
      if (this.dic_edit_form.type === 'edit') {
        update(this.dic_edit_form).then(response => {
          this.dicEditVisible = false
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.getDicList()
        })
      }
    },
    // 修改回显
    dicUpdate(data) {
      this.dicEditVisible = true
      this.dic_edit_form.dic_code = data.dic_code
      this.dic_edit_form.dic_pcode = data.dic_pcode
      this.dic_edit_form.dic_name = data.dic_name
      this.dic_edit_form.dic_desc = data.dic_desc
      this.dic_edit_form.dic_id = data.dic_id
      this.dic_edit_form.type = 'edit'
    },
    // 同步至缓存
    dicCache() {
      this.$confirm('确认要同步至缓存吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        cache().then(response => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.getDicList()
        })
      }).catch(() => {})
    },
    // 停用字典
    dicDisable(dic_id) {
      this.$confirm('确认要停用该字典吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        disable(dic_id).then(response => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.getDicList()
        })
      }).catch(() => {})
    },
    // 启用字典
    dicEnable(dic_id) {
      this.$confirm('确认要启用该字典吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        enable(dic_id).then(response => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.getDicList()
        })
      }).catch(() => {})
    }
  },
  created() {
    this.permissionAuth()
    this.getDicList()
  }
}
</script>
<style lang="scss">
.font_green{
  color: #5FB878;
}
.font_red{
  color: #FF5722;
}
</style>
